import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import Immutable from 'immutable';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';

const init = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: action.message,
                brands: action.data.brands,
                isDealerUser: action.data.isDealerUser,
                marketDepartments: action.data.marketDepartments
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: '',
            });
        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                hasError: '',
                data: action.data.content
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                total: 1,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};

const appState = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.setIn(['queryCondition', action.name], action.value);
        case actions.GET_INIT_DATA_SUCCESS:
            return state.withMutations(map =>
                map
                    .setIn(['queryCondition', 'marketDepartmentId'], action.data.marketDepartments.id)
                    .set('queryMarke', action.data.marketDepartments)
            );
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        case actions.GET_LIST_DATA_SUCCESS:
            return state.set('queryBySort', Immutable.fromJS(action.condition));
        case actions.SAVE_QUERY_AREA:
            return state.set('queryArea', action.data);
        case actions.SAVE_QUERY_DEALER:
            return state.set('queryDealer', action.data);
        case actions.SAVE_QUERY_MARKE:
            return state.set('queryMarke', action.data);
        default:
            return state;
    }
};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const domainData = combineReducers({
    init,
    permission,
    list
});
const ui = (state, action) => {
    switch(action.type) {
        case actions.START_BATCH_SX:
            return state.set('showBatchSXPanel', true);
        case actions.CLEAR_BATCH_SX:
            return state.set('showBatchSXPanel', false);
        case actions.START_BATCH_ZF:
            return state.set('showBatchZFPanel', true);
        case actions.CLEAR_BATCH_ZF:
            return state.set('showBatchZFPanel', false);
        case actions.START_BATCH_GD:
            return state.set('showBatchGDPanel', true);
        case actions.CLEAR_BATCH_GD:
            return state.set('showBatchGDPanel', false);
        default:
            return state;
    }
};

export default combineReducers({
    domainData,
    appState,
    ui
});
